define(['jquery'],($)=>{
    class Header{
        constructor(){
            this.loadHTML().then(()=>{
                this.loginCheck()
                this.search()
                this.calcCartTotal()
                this.goCart()
                // this.User()
                this.headergotomall()
            })
            
        }
        loadHTML(){
            return new Promise(resolve =>{
                $('header').load('/html/modules/header.html',resolve)
            })
        }


        loginCheck(){
            var getcookie = document.cookie
            if(getcookie){
                const cookie=document.cookie.split('=')
                const username=cookie[1]
                $('#userInfo').html(`你好，${username}`)
                this.logout()

                
            }
            else{
                $('#userInfo').html('点击登录')
                this.User()
                
            }



            
        }



        search(){
            $('#searchInput').on('focus',()=>{
                $('#searchResult').css('display','block')
                
                $('#searchInput').on('keyup',function(){
                    $.getJSON('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?',{wd :$(this).val()},function(resp){
                        const { s } = resp
                        $('#searchResult').html(s.reduce((html,item)=>{
                            html+= `<dd>${item}</dd>`
                            return html
                        },''))
                        $('#searchResult dd').on('mousedown',function(){
                            $('#searchInput').val($(this).html())
                        })
                    })
                })
                
            })
            $('#searchInput').on('blur',()=>{
                $('#searchResult').css('display','none')
            })
        }
        calcCartTotal(){
            let count =0
            let cart = localStorage.getItem('cart')
            if(cart){
                // console.log(cart)
                cart=JSON.parse(cart)
                count = cart.reduce((total,item)=>{
                    return total+item.count
                },0)
            }
            $('#headerShopNum').html(count)
            $('#shopCartNum').html(count)
        }
        goCart(){
            $('#goCartIndex').on('click',()=>{
                location.replace('/html/shopping.html')
            })
        }
        User(){
            $('#User').on('click',()=>location.replace('/html/login.html'))
        }
        logout(){
            $('#User').on('click',()=>{
                if(confirm('确定退出登录吗？')){
                    const d = new Date()
                    d.setDate(d.getDate()-1)
                    document.cookie=`username=1;expires=${d.toUTCString()};path=/`
                    location.reload()
                }
            })
        }
        headergotomall(){
            $('#headergotomall').on('click',()=>location.replace('/html/mall.html'))
        }
        
    }
    return new Header()
})